// 使用js-cookie库来操作cookie
import Cookies from 'js-cookie';
import aes from './aes';

// 定义 CookieOptions 接口
interface CookieOptions {
  expires?: number | Date; // 过期时间，可以是数字（以天为单位）或日期对象
  path?: string; // 访问路径
  domain?: string; // 域名
  secure?: boolean; // 是否仅在 HTTPS 下传输
  sameSite?: 'strict' | 'lax' | 'none'; // SameSite 属性
}

// 定义 cookie 对象及其方法的类型
interface Cookie {
  set(key: string, value: string, options?: CookieOptions): void;
  get(key: string): string | undefined;
  remove(key: string, options?: CookieOptions): void;
}

const cookie: Cookie = {
  set(key: string, value: string, options: CookieOptions = { expires: 30 }) {
    Cookies.set(key, aes.encrypted(value), options);
  },
  get(key: string) {
    return aes.decrypted(Cookies.get(key) || '');
  },
  remove(key: string, options?: CookieOptions) {
    Cookies.remove(key, options);
  },
};

export default cookie;
